unit Conn;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, SQLHelpers, StdCtrls, Buttons, ExtCtrls, ComCtrls, DB,
  DBClient;

type
  TConnForm = class(TForm)
    Panel1: TPanel;
    BitBtn1: TBitBtn;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    cbConnType: TComboBox;
    Splitter1: TSplitter;
    edConnStrings: TMemo;
    Label2: TLabel;
    edConnName: TEdit;
  private
    class procedure AddConnNode(lv: TListView; ConnName: string);
    class function SetConnConfig(NewConn: Boolean;
      AConnName: string): string;
  public
    ConnName, ConnType, ConnStrings: string;
    class procedure LoadConnections(lv: TListView; ds: TClientDataSet);
    class function AddConnConfig(lv: TListView): string;
    class procedure EditConnConfig(lv: TListView; AConnName: string);
  end;


implementation

{$R *.dfm}

{ TConnForm }

class function TConnForm.AddConnConfig(lv: TListView): string;
begin
  Result := SetConnConfig(True, '');
end;

class procedure TConnForm.LoadConnections(lv: TListView;
  ds: TClientDataSet);
begin
  DBHelper.ExecuteQuery(ds, 'select * from tConnects', nil, []);
end;

class function TConnForm.SetConnConfig(NewConn: Boolean;
  AConnName: string): string;
var
  ConnForm: TConnForm;
  par: TPersistent;
begin
  ConnForm := TConnForm.Create(Application);
  try
    if ConnForm.ShowModal = mrOK then
    begin
      ConnForm.ConnName := ConnForm.edConnName.Text;
      ConnForm.ConnType := ConnForm.cbConnType.Text;
      ConnForm.ConnStrings := ConnForm.edConnStrings.Text;
      Result := ConnForm.ConnName;

      par := DBHelper.CreateParams;
      try
        DBHelper.AddParams(par, 'ConnCode', ftString, ConnForm.ConnName);
        DBHelper.AddParams(par, 'ConnType', ftString, ConnForm.ConnType);
        DBHelper.AddParams(par, 'ConnectionStrings', ftString, ConnForm.ConnStrings);
        DBHelper.ExecuteSQL('INSERT INTO tConnects (ConnCode, ConnType, ConnectionStrings)' +
          ' VALUES(:ConnCode, :ConnType, :ConnectionStrings)', par);
      finally
        par.Free;
      end;
    end;
  finally
    ConnForm.Free;
  end;
end;

class procedure TConnForm.EditConnConfig(lv: TListView;
  AConnName: string);
begin
  SetConnConfig(False, AConnName);
end;

class procedure TConnForm.AddConnNode(lv: TListView; ConnName: string);
begin

end;

end.
